package com.twitterbuzz.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.twitterbuzz.shared.UserInfoClient;

public class LoginScreen extends Composite {
	private VerticalPanel mainPanel;
	private HorizontalPanel headPanel;
	private VerticalPanel paddingPanel;	
	private HorizontalPanel usernamePanel;
	private HorizontalPanel passwordPanel;
	// TextBox for the User Name
	private TextBox signInLogin;
	private TextBox signUpLogin;
	// PasswordTextBox for the password
	private PasswordTextBox signInPassword;
	private PasswordTextBox signUpPassword;
	private Button signIn;
	private Button signUp;
	private HorizontalPanel loginPanel;
	private HorizontalPanel titlePanel;
	private Label appName;
	private Label errorLabel;
	private Label usernameError;
	private Label passwordError;
	private Label welcomeMsg;
	
	public LoginScreen() {
		mainPanel = new VerticalPanel();
		headPanel = new HorizontalPanel();
		signInLogin = new TextBox();
		signInLogin.setStyleName("textbox");			
		signInPassword = new PasswordTextBox();
		signInPassword.setStyleName("textbox");		
		signIn = new Button("Sign in");		
		signIn.setStyleName("button");
		loginPanel = new HorizontalPanel();
		titlePanel = new HorizontalPanel();
		appName = new Label("Visual tweet buzzer");
		appName.setStyleName("title");
		titlePanel.add(appName);
		loginPanel.setSpacing(5);
		loginPanel.add(signInLogin);
		loginPanel.add(signInPassword);
		loginPanel.add(signIn);	
		loginPanel.setStyleName("paddingtop");
		headPanel.add(loginPanel);
		headPanel.add(titlePanel);
		paddingPanel = new VerticalPanel();
		paddingPanel.setStyleName("padding");
		paddingPanel.add(headPanel);
		errorLabel = new Label();
		errorLabel.setStyleName("error");
		errorLabel.setVisible(false);
		paddingPanel.add(errorLabel);
		mainPanel.add(paddingPanel);
		welcomeMsg = new Label("New to Visual tweet buzzer? Join today!");
		welcomeMsg.setStyleName("label");
		signUpLogin = new TextBox();			
		signUpLogin.setStyleName("longtextbox");
		signUpPassword = new PasswordTextBox();
		signUpPassword.setStyleName("longtextbox");
		signUp = new Button("Sign up");	
		signUp.setStyleName("longbutton");
		
		usernamePanel = new HorizontalPanel();
		usernamePanel.setSpacing(5);
		usernamePanel.add(signUpLogin);
		usernameError = new Label();
		usernameError.setVisible(false);
		usernameError.setStyleName("error");
		usernamePanel.add(usernameError);
		passwordPanel = new HorizontalPanel();
		passwordPanel.setSpacing(5);
		passwordPanel.add(signUpPassword);
		passwordError = new Label();
		passwordError.setVisible(false);
		passwordError.setStyleName("error");
		passwordPanel.add(passwordError);
		
		mainPanel.setSpacing(5);
		mainPanel.add(welcomeMsg);
		mainPanel.add(usernamePanel);
		mainPanel.add(passwordPanel);
		mainPanel.add(signUp);
		mainPanel.setStyleName("panel");
		
		signIn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				clearErrors();
				checkLogin(signInLogin.getText(), signInPassword.getText());
			}
		});
		
/*		signUp.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				clearErrors();
				register(signUpLogin.getText(), signUpPassword.getText());
			}
		});*/
		
		initWidget(mainPanel);
	}
	
	private void checkLogin(String userName, String password) {		
		TwitterBuzzServiceAsync nbService = (TwitterBuzzServiceAsync) GWT.create(TwitterBuzzService.class);
		AsyncCallback<UserInfoClient> callback = new AsyncCallback<UserInfoClient>() {
			@Override
			public void onSuccess(UserInfoClient user) {
				if (user == null) {
					errorLabel.setText("Wrong Username and password combination");
					errorLabel.setVisible(true);
				}
				else 
					setHomeScreen(user);					
			}
			@Override
			public void onFailure(Throwable caught) {
				errorLabel.setText("Wrong Username and password combination");
				errorLabel.setVisible(true);
			}
		};
		nbService.authenticateUser(userName, password, callback);
	}
	/*
	private void register(String userName, String password) {		
		if (userName.isEmpty() || password.isEmpty()) {
			usernameError.setText("Please provide your twitter username/password combination");
			usernameError.setVisible(true);			
		}		
		this.userName = userName;
		TwitterBuzzServiceAsync nbService = (TwitterBuzzServiceAsync) GWT
				.create(TwitterBuzzService.class);
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onSuccess(String url) {
				if (url == null) {
					usernameError.setText("Please provide your twitter username/password combination");
					usernameError.setVisible(true);
				} else {
					setAuthScreen(url);
				}					
			}

			public void onFailure(Throwable caught) {
				usernameError.setText("Please provide your twitter username/password combination");
				usernameError.setVisible(true);
			}
		};

		nbService.registerUser(userName, password, callback);
	} */
	
	public void setAuthScreen(String url) {
		AuthScreen authScreen = new AuthScreen(url);
		RootPanel.get().clear();
		RootPanel.get().add(authScreen);
	}
	
	public void clearErrors() {
		errorLabel.setText("");
		usernameError.setText("");
		passwordError.setText("");
	}
	
	public void setHomeScreen(UserInfoClient user) {
		HomeScreen homeScreen = new HomeScreen(user);
		RootPanel.get().clear();
		RootPanel.get().add(homeScreen);
	}
}